package cn.stylefeng.guns.modular.robot.service.impl;

import cn.stylefeng.guns.core.common.exception.BizExceptionEnum;
import cn.stylefeng.guns.core.shiro.ShiroKit;
import cn.stylefeng.guns.core.shiro.ShiroUser;
import cn.stylefeng.guns.modular.robot.entity.DfInfo;
import cn.stylefeng.guns.modular.robot.entity.Group;
import cn.stylefeng.guns.modular.robot.entity.dto.DfImportDto;
import cn.stylefeng.guns.modular.robot.entity.enums.GroupType;
import cn.stylefeng.guns.modular.robot.listener.DfImportListener;
import cn.stylefeng.guns.modular.robot.mapper.DfInfoMapper;
import cn.stylefeng.guns.modular.robot.service.DfInfoService;
import cn.stylefeng.guns.modular.robot.service.GroupService;
import cn.stylefeng.guns.modular.robot.utils.RobotUtils;
import cn.stylefeng.roses.kernel.model.exception.ServiceException;
import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.web.multipart.MultipartFile;
import org.telegram.telegrambots.bots.TelegramLongPollingBot;

import javax.annotation.Resource;
import java.io.IOException;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Objects;

@Service
@Slf4j
public class DfInfoServiceImpl extends ServiceImpl<DfInfoMapper, DfInfo> implements DfInfoService {

    @Resource
    private TelegramLongPollingBot bot;
    @Resource
    private GroupService groupService;

    @SneakyThrows
    @Override
    public void dfNotify(Long id, String message, MultipartFile file) {
        DfInfo dfInfo = getById(id);
        if (Objects.isNull(dfInfo)) {
            return;
        }
        dfInfo.setIsHandle(true);
        dfInfo.setMemo(message);
        dfInfo.setUpdateTime(new Date());
        updateById(dfInfo);
        BigDecimal amount = dfInfo.getAmount();
        String account = dfInfo.getAccount();
        String msg = "收款账户：" + account + "\n收款金额：" + amount + "\n";
        RobotUtils.sendPhoto(dfInfo.getGroupId(), dfInfo.getMessageId(), msg + message, file.getInputStream(), file.getOriginalFilename(), bot);
    }

    @Override
    public void importDf(MultipartFile file) {
        try {
            DfImportListener dfImportListener = new DfImportListener(this);
            EasyExcel.read(file.getInputStream(), DfImportDto.class, dfImportListener).doReadAll();
            List<DfImportDto> dfImportDtos = dfImportListener.getDfImportDtos();
            addBatch(dfImportDtos);
        } catch (IOException e) {
            log.error("excel解析异常", e);
        }
    }

    @Override
    public void addBatch(List<DfImportDto> dfImportDtos) {
        ShiroUser shiroUser = ShiroKit.getUserNotNull();
        String account = shiroUser.getAccount();
        List<Group> list = groupService.lambdaQuery()
                .eq(Group::getCreateBy, account)
                .eq(Group::getType, GroupType.DF)
                .list();
        if (CollectionUtils.isEmpty(list)) {
            throw new ServiceException(BizExceptionEnum.DF_GROUP_NOT_EXISTS);
        }
        Group group = list.get(0);
        List<DfInfo> dfInfos = new ArrayList<>();
        for (DfImportDto dfImportDto : dfImportDtos) {
            DfInfo dfInfo = new DfInfo();
            dfInfo.setGroupId(group.getTgGroupId());
            dfInfo.setGroupName(group.getName());
            dfInfo.setGroupUserName(group.getCreateBy());
            dfInfo.setIsHandle(false);
            dfInfo.setAccount(dfImportDto.getAccount());
            dfInfo.setAmount(new BigDecimal(dfImportDto.getAmount()));
            dfInfo.setCreateTime(new Date());
            dfInfo.setCreateBy(account);
            dfInfos.add(dfInfo);
        }
        saveBatch(dfInfos);
    }

}
